<?php
namespace app\wechat\controller;

use app\dsc\controller\BaseController;
use tp5_lib\wechat\utility\WechatOAuth;

/**
 * 单独测试`WechatOAuth`,没有关联本身的用户系统
 */
class OAuthTestController extends BaseController
{
    /**
     * 判断是否是在微信中打开，并跳转到相应的页面
     */
    public function index()
    {
        $ua=$this->request->header("user-agent");
        if(strpos($ua,"MicroMessenger")){
            $this->authorize();
        }
        else{
            $this->qrconnect();
        }
    }
    /**
     * 扫码登录页面
     */
    public function qrconnect()
    {
        $config=$this->getConfig("web");
        $url=WechatOAuth::alloc($config["AppID"],$config["AppSecret"])
            ->getScanLoginUrl("http://www.airbaba.cn/custom/wechat_cb.php","423423523");
        $this->redirect($url);
    }
    /**
     * 接收code页面
     */
    public function qrconnectCode()
    {
        header("Access-Control-Allow-Origin:*");
        $code=input("code");
        $config=$this->getConfig("web");
        $wechat=WechatOAuth::alloc($config["AppID"],$config["AppSecret"]);
        return $this->showUserInfo($wechat,$code);
    }
    /**
     * 网页授权页面
     */
    public function authorize()
    {
        $scope=input("scope","snsapi_userinfo");
        $config=$this->getConfig("mp");
        $url=WechatOAuth::alloc($config["AppID"],$config["AppSecret"])
            ->getWebpageAuthUrl("http://www.airbaba.cn/custom/wechat_cb.php",$scope,"423423523");
        $this->redirect($url);
    }
    /**
     * 接收code页面
     * 需要检查重复发送的code
     */
    public function authorizeCode()
    {
        $code=input("code");
        $config=$this->getConfig("mp");
        $wechat=WechatOAuth::alloc($config["AppID"],$config["AppSecret"]);
        return $this->showUserInfo($wechat,$code);
    }
    protected function showUserInfo($wechat,$code)
    {
        echo "<pre>";
        $token_info=$wechat->getAccessToken($code);
        print_r($token_info);
        if(isset($token_info["errcode"])){
            exit;
        }
        $user_info=$wechat->getUserInfo($token_info["openid"],$token_info["access_token"]);
        print_r($user_info);
        echo "</pre>";
    }
    /**
     * 检查AccessToken是否可用
     */
    public function checkToken()
    {
        $openid=input("openid");
        $access_token=input("token");

        $name=input("name");
        $config=$this->getConfig($name);
        $wechat=WechatOAuth::alloc($config["AppID"],$config["AppSecret"]);
        $check_result=$wechat->checkAccessToken($openid,$access_token);
        echo "<pre>";
        print_r($check_result);
        echo "</pre>";
    }
    /**
     * 刷新AccessToken
     */
    public function refreshToken()
    {
        $refresh_token=input("token");
        
        $name=input("name");
        $config=$this->getConfig($name);
        $wechat=WechatOAuth::alloc($config["AppID"],$config["AppSecret"]);
        $token_info=$wechat->refreshToken($refresh_token);
        echo "<pre>";
        print_r($token_info);
        echo "</pre>";
    }
    protected function getConfig($name)
    {
        $config_list=[
            "web"=>["AppID"=>"wx64c9c489b30b989d","AppSecret"=>"0795f080a7a8f7b1a45a46320ed38ed3"],
            "mp"=>["AppID"=>"wx708c724839b0f05c","AppSecret"=>"03b0f5a0a42be246c6b46b23c08883fa","EncodingAESKey"=>"CryKohUVjsCzlZqY919Vw1TsYQmX0fqi58ox753fQHC"],
        ];
        return $config_list[$name];
    }
}
